package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"shang/conmon"
	"shang/models"
)

type RegController struct {
	beego.Controller
}

func (r *RegController) Reg() {
	username := r.GetString("username")
	password := r.GetString("password")
	user, _ := models.GetUserInfo(models.User12{
		Username: username,
	})
	if user.Id != 0 {
		r.Data["json"] = map[string]interface{}{
			"code":   400,
			"msg":    "账号已被注册",
			"userid": 0,
		}
		r.ServeJSON()
		return
	}
	m := conmon.Md5(password)
	u, err := models.CreateUser(models.User12{
		Username: username,
		Password: m,
	})
	if err != nil {
		r.Data["json"] = map[string]interface{}{
			"code":   400,
			"msg":    "注册失败",
			"userid": 0,
		}
		r.ServeJSON()
		return
	}
	r.Data["json"] = map[string]interface{}{
		"code":   200,
		"msg":    "注册成功",
		"userid": u.Id,
	}
	r.ServeJSON()
	return
}
